package xyz.yuanjilu.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.editor.language.json.converter.BpmnJsonConverter;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.Model;
import org.activiti.rest.service.api.repository.BaseModelSourceResource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import xyz.yuanjilu.utils.R;

/**
 *完善资源接口的功能
 * 
 * @author chenjunfei
 * @email kkomge@qq.com
 * @date 2017-09-21 10:40:56
 */
@RestController
public class MyModelSourceResource extends BaseModelSourceResource {

  
  @RequestMapping(value="/repository/models/{modelId}/source", method = RequestMethod.POST)
  protected void setModelSource(@PathVariable String modelId, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
    Model model = getModelFromRequest(modelId);
    if (model != null) {
      
    	 ObjectMapper objectMapper = new ObjectMapper();
	      ObjectNode editorNode = objectMapper.createObjectNode();
	      editorNode.put("id", "canvas");
	      editorNode.put("resourceId", "canvas");
	      ObjectNode stencilSetNode = objectMapper.createObjectNode();
	      stencilSetNode.put("namespace", "http://b3mn.org/stencilset/bpmn2.0#");
	      editorNode.put("stencilset", stencilSetNode);
	      repositoryService.addModelEditorSource(model.getId(), editorNode.toString().getBytes("utf-8"));
    }
  }
  @RequestMapping(value="/repository/models/{modelId}/deploye", method = RequestMethod.POST)
  protected R deploye(@PathVariable String modelId) throws JsonProcessingException, IOException {
    Model model = getModelFromRequest(modelId);
    R ok = R.ok();
    if (model != null) {
      
    	ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(repositoryService.getModelEditorSource(model.getId()));  
        byte[] bpmnBytes = null;  
        BpmnModel bmpModel = new BpmnJsonConverter().convertToBpmnModel(modelNode);  
        bpmnBytes = new BpmnXMLConverter().convertToXML(bmpModel);  
        String processName = model.getName() + ".bpmn20.xml";  
        Deployment deployment = repositoryService.createDeployment()  
                .name(model.getName())  
                .addString(processName, new String(bpmnBytes)).deploy();  
        if (deployment != null && deployment.getId() != null) {  
        	ok.put("deploymentId", deployment.getId());
        }else{
        	ok=R.error();
        }
    }
	return ok;
  }
  @RequestMapping(value="/repository/models/{modelId}/download", method = RequestMethod.GET)
  protected void download(@PathVariable String modelId,HttpServletResponse response) throws JsonProcessingException, IOException {
    Model model = getModelFromRequest(modelId);
    if (model != null) {
    	ServletOutputStream outputStream = response.getOutputStream();
    	ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(repositoryService.getModelEditorSource(model.getId()));  
        byte[] bpmnBytes = null;  
        BpmnModel bmpModel = new BpmnJsonConverter().convertToBpmnModel(modelNode);  
        bpmnBytes = new BpmnXMLConverter().convertToXML(bmpModel);  
        String processName = model.getName() + ".bpmn20.xml";  
        //设置Content-Disposition  
        response.setHeader("Content-Disposition", "attachment;filename="+processName);          
        outputStream.write(bpmnBytes);
       
    }
  }
}
